﻿package domino 
{
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.*;
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	/**
	 * ...
	 * @author ...
	 */
	public class IOXML
	{
     private var file_data:XML;
	 private var levels:XMLList;
	
	 public var typeOfFigure:String;


	public function IOXML(file:XML) {
		file_data = null;
		file_data = file;	
		};
			
	public function getFigures(i:int):Array {		 
	  //figures = null;
	   var stat:Boolean;
	   levels = null;
	   list = null;
	   levels = file_data..stage.(@number == i).*;
	//	trace();
		var figures:Array = new Array();	
		    figures = [];
			
			if (levels.parent())
			 for each(var list:XML in levels ) {
				 typeOfFigure = list.@type;
			 var A:Array = [list.x, list.y, typeOfFigure,list.angle,list.density,list.friction,list.scaleX,list.scaleY,levels.parent().@coins,list.restitution,list.angleVelocity,list.sensor,levels.parent().@clicks];
				 figures.push(A);
				}
			   // trace(figures.length);
			    return figures
		};//end
		
		public function saveLevel(figures:Array, level:int,clicks:Number) {
			var coins:int=0;
			var j:int = 0;
			var FR:FileReference = new FileReference();
			var lev:XMLList = file_data..stage.(@number == level);
		//	 trace(lev + " Lev");
		     delete lev.*;
			  lev.@number_domin = level;
             
			//	trace(figures.length + " length");
			for (var i:int = 0; i < figures.length; i++ )
			  {
				  
			    //if ((figures[i] is actorDomino)||(figures[i] is actorAngle)||(figures[i] is actorCircle)||(figures[i] is actorCross)||(figures[i] is actorCoin)) 
				if (figures[i] is actor) {	
					if (figures[i] is actorWall)
					   continue;
						
					  var shape:XML =<shape num="1" type="domino"> 
			          <x>0</x>
			          <y>0</y>
			          <angle>0</angle>
					  <density>0</density>
					  <friction>0</friction>
					  <scaleX>1</scaleX>
					  <scaleY>1</scaleY>
					  <restitution>0</restitution>
					  <angleVelocity>0</angleVelocity>
					  <sensor>false</sensor>
					  
					  </shape> ;
					  shape.@num = j;
			          ///////////// type
					  if(figures[i] is actorDomino) 
					   shape.@type = "domino";
					  if(figures[i] is actorTriangle) 
					   shape.@type = "triangle"; 
					   if(figures[i] is actorAngle) 
					   shape.@type = "angle";
					  if (figures[i] is actorCircle)
					    shape.@type = "circle";
					  if (figures[i] is actorCross)
					    shape.@type = "cross";
					  if (figures[i] is actorTrident)
					    shape.@type = "trident";	
					  if (figures[i] is actorTrident2)
					    shape.@type = "trident2";
					  if (figures[i] is actorPendulum)
					    shape.@type = "pendulum";	
					  if (figures[i] is actorPendulum2)
					    shape.@type = "pendulum2";	
					  		
					  if (figures[i] is actorCoin)
					  {
					    shape.@type = "coin";
						if(figures[i].friction!=0)
						 coins++;
					  }	
					  
					 
					  ////////////// friction
					   shape.friction = figures[i]._body.GetShapeList().GetFriction();
					  ////////////// density
					   shape.density = figures[i].density;
					  ///////////// scaleX
					   shape.scaleX = figures[i].scaleX;
					  ///////////// scaleY
					   shape.scaleY = figures[i].scaleY;
					   
					
					 ///////////// coordinates	
				      shape.x = figures[i]._costume.x;
			          shape.y = figures[i]._costume.y;
					  /////////////angle
			          shape.angle = figures[i]._costume.rotation;
					  /////////restitution
				      shape.restitution = figures[i]._body.GetShapeList().GetRestitution();
					  shape.angleVelocity = figures[i].angV;
					  shape.sensor = figures[i].isSensor;
					  
				     for each(var x:XML in lev)
				      lev.prependChild(shape);
					   //trace(lev);
					  //lev.prependChild(shape.x);
					  j++;
					}
				}
			    lev.@number_domin = j;
				lev.@coins = coins;
				lev.@clicks = clicks;
				
				
				FR.save(file_data,"test.xml"); 
			}
			
			
	}

}